/*
 * Copyright (C) 2011 Nanohex Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nanohexcorp.kudocode.snapqr.data;

import com.nanohexcorp.kudocode.snapqr.db.QRLinksHelper;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

/**
 * This class does all SQL interaction with the database.
 * Its parent class contains all the enumerations.
 */
public class QRLinksData extends QRLinksHelper {
	// Database fields
	private SQLiteDatabase database;
	private SQLiteStatement insertStatement;

	// constructor
	public QRLinksData(SQLiteDatabase db) {
		super();
		this.database = db;
		this.insertStatement = database.compileStatement(INSERT);
	}

	// insert using compiled statements
	protected long insert(String title, String url, String description) {
		this.insertStatement.bindString(1, title);
		this.insertStatement.bindString(2, url);
		this.insertStatement.bindString(3, description);
		return this.insertStatement.executeInsert();
	}

	// update using standard
	protected boolean update(long id, String title, String url, String description) {
		ContentValues updateValues = createContentValues(title, url, description);
		return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + id, null) > 0;
	}

	// delete record based on id
	protected boolean delete(long id) {
		return database.delete(QRLinksHelper.DATABASE_TABLE, KEY_ROWID + "=" + id, null) > 0;
	}

	// delete record based on id
	protected boolean deleteAll() {
		return database.delete(QRLinksHelper.DATABASE_TABLE, null, null) > 0;
	}

	// select all records
	protected Cursor selectAll() {
		Cursor mCursor = database.query(QRLinksHelper.DATABASE_TABLE,
				new String[] { KEY_ROWID, KEY_TITLE, KEY_URL, KEY_DESCRIPTION },
				null, null, null, null, KEY_TITLE);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// select record based on id
	protected Cursor selectByID(long rowId) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE,
				new String[] { KEY_ROWID, KEY_TITLE, KEY_URL, KEY_DESCRIPTION },
				KEY_ROWID + "=" + rowId, null, null, null, KEY_TITLE, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	// used for internal functions
	private ContentValues createContentValues(String title, String url, String description) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, title);
		values.put(KEY_URL, url);
		values.put(KEY_DESCRIPTION, description);
		return values;
	}
}
